package com.super8.super8newtv;

import androidx.appcompat.app.AppCompatActivity;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;

import android.os.Build;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;


import android.view.WindowManager;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.super8.super8newtv.utils.LogUtils;


import butterknife.BindView;
import butterknife.ButterKnife;

public class TVActivity extends Activity {

    @BindView(R.id.web_view)
    WebView webView;

//    @BindView(R.id.shopping_web_view)
//    BridgeWebView webView;
    private WebSettings settings;

    private WindowManager windowManager;
    private View fullScreenView = null;

    String pageUrl = "https://xiaobaotv.net/";

    @Override
    protected void onResume() {
        super.onResume();
        // 如果之前处于全屏状态，重新进入后需要再次调用全屏
        if (fullScreenView != null) fullScreen(fullScreenView);
    }


    private void fullScreen(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        } else {
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }



    @Override
    public void onBackPressed() {
        // 全屏状态点击返回退出全屏
        if (fullScreenView != null) {
            windowManager.removeViewImmediate(fullScreenView);
            fullScreenView = null;
        } else {
            super.onBackPressed();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tvactivity);
        ButterKnife.bind(this);

        windowManager = getWindowManager();
         pageUrl =  getIntent().getStringExtra("url");
        LogUtils.e("1111111","==========="+pageUrl+"=====");
        if (TextUtils.isEmpty(pageUrl)) {
//            pageUrl="https://xn--44qz85a01qpc.com/";
            pageUrl="http://tool.liumingye.cn/m3u8/#https://cdn15.yzzy-kb-cdn.com/20230530/14215_9018f11f/index.m3u8";
        }else if("1080".trim().equals(pageUrl))
            pageUrl="https://www.1080zyk1.com/";
        else if("1000".trim().equals(pageUrl)) {
            pageUrl = "https://xiaobaotv.net/";
        }







        initWebView();
//        webView.loadUrl(imageUrl);
    }

    /**
     * 初始化webview
     */
    private void initWebView() {
        //支持js
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setJavaScriptEnabled(true);

        settings.setDomStorageEnabled(true);//设置适应Html5的一些方法
        settings.setGeolocationEnabled(true);

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //不使用缓存
        settings.setDefaultTextEncodingName("utf-8"); //避免中文乱码

        //设置适应屏幕NORMAL   1.NARROW_COLUMNS：可能的话使所有列的宽度不超过屏幕宽度  2.NORMAL：正常显示不做任何渲染 3.SINGLE_COLUMN：把所有内容放大webview等宽的一列中
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setUseWideViewPort(true);//设置webview推荐使用的窗口
        settings.setLoadWithOverviewMode(true);//设置webview加载的页面的模式

        // 解决5.0图片不显示问题
        settings.setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);  //硬件加速
        String userAgent = settings.getUserAgentString();
        String newuserAgent = userAgent + " oadzApp";
        settings.setUserAgentString(newuserAgent);
//        webView.setWebViewClient(new WebViewClient());
        if (Build.VERSION.SDK_INT >= 19) {
            webView.getSettings().setLoadsImagesAutomatically(true);
        } else {
            webView.getSettings().setLoadsImagesAutomatically(false);
        }

        webView.loadUrl(pageUrl);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, final String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);

            }

            //配置定位权限
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }



            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {
                // 此处的 view 就是全屏的视频播放界面，需要把它添加到我们的界面上
                windowManager.addView(view, new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION));
                // 去除状态栏和导航按钮
                fullScreen(view);
                fullScreenView = view;
            }

            @Override
            public void onHideCustomView() {
                // 退出全屏播放，我们要把之前添加到界面上的视频播放界面移除
                windowManager.removeViewImmediate(fullScreenView);
                fullScreenView = null;
            }


        });
        webView.setWebViewClient(new TVActivity.MyWebViewClient());
    }

    /**
     * 初始化webview
     */
    private void initWebView1() {
        //支持js
        settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通過JS打開新窗口
        settings.setJavaScriptEnabled(true);//支持js

        settings.setDomStorageEnabled(true);//设置适应Html5的一些方法
        settings.setGeolocationEnabled(true);//啟用地理定位

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//webview中緩存设置--不缓存Don't use the cache, load from the network.
        settings.setDefaultTextEncodingName("utf-8"); //避免中文乱码

        //设置适应屏幕NORMAL
        //NARROW_COLUMNS
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setLoadWithOverviewMode(true);
//        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //不使用缓存
        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);  //硬件加速
        String userAgent = settings.getUserAgentString();
        String newuserAgent = userAgent + " oadzApp";
        settings.setUserAgentString(newuserAgent);
        webView.setWebChromeClient(new WebChromeClient(){
            /**
             * @param view
             * @param requestedOrientation
             * @param callback
             * @deprecated
             */
            @Override
            public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
                super.onShowCustomView(view, requestedOrientation, callback);
            }


        });
        webView.setWebViewClient(new MyWebViewClient());
        if (Build.VERSION.SDK_INT >= 19) {
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }


    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String loadUrl, Bitmap favicon) {

            super.onPageStarted(view, loadUrl, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!webView.getSettings().getLoadsImagesAutomatically()) {
                webView.getSettings().setLoadsImagesAutomatically(true);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
            return super.shouldOverrideUrlLoading(view, url);
        }


    }
    class MyWebViewClient1 extends BridgeWebViewClient {
        public MyWebViewClient1(BridgeWebView webView) {
            super(webView);
        }

        @Override
        public void onPageStarted(WebView view, String loadUrl, Bitmap favicon) {

            super.onPageStarted(view, loadUrl, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!webView.getSettings().getLoadsImagesAutomatically()) {
                webView.getSettings().setLoadsImagesAutomatically(true);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
            return super.shouldOverrideUrlLoading(view, url);
        }


    }


}